/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-04-09
 * Time: 22:33
 */
public class HashBuck2 <K,V>{
    static class Node<K,V>{
        public K key;
        public V value;
        public Node<K,V> next;

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
        }
    }
    public Node<K,V>[] array = new Node[10];
    public int usedSize;
    public static final double  LOAD_FACTOR = 0.75;

    public void push(K key, V value) {
        //1.找到位置
        Node<K,V> node = new Node(key,value);
        int hash = key.hashCode();
        int index = hash % array.length;
        //2.遍历该位置的链表
        Node<K,V> cur = array[index];
        while(cur != null) {
            //更新value
            if(cur.key.equals(key)) {
                cur.value = value;
            }
            cur = cur.next;
        }
        //尾插法
        node.next = array[index];
        array[index] = node;
        usedSize++;
        //3.判断负载因子
        if(doLoadFactor() >= LOAD_FACTOR) {
            //重新哈希
        }
    }
    private double doLoadFactor() {
        return usedSize * 1.0 / array.length;
    }
    public V get(K key) {
        int hash = key.hashCode();
        int index = hash % array.length;
        //2.遍历该位置的链表
        Node<K, V> cur = array[index];
        while (cur != null) {
            if (cur.key.equals(key)) {
                return cur.value;
            }
            cur = cur.next;
        }
        return null;
    }
}
